<?php
/**
 * 系统公共函数库
 * @author wangXi <iwisunny@gmail.com>
 * Date: 2015/12/1 11:37
 */

/**
 * php版本判断
 * @param string $version
 * @return mixed
 */
function check_php_version($version = '5.3.0')
{
    static $_is_php;
    $version = (string)$version;

    if (!isset($_is_php[$version])) {
        $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? false : true;
    }

    return $_is_php[$version];
}

/**
 * 打印变量
 * @param      $var
 * @param bool $as_string
 * @return mixed
 */
function dd($var, $as_string = false)
{
    if ($as_string === true) {
        return var_export($var, true);
    }
    var_dump($var);
    exit;
}

/**
 * 加载器 模拟python的import
 * @param              $class_name
 * @param string|array $from_where
 * @return bool
 */
function import($class_name, $from_where = '')
{
    if (is_string($class_name)) {
        $class_name = ltrim(str_replace('\\', '/', $class_name), '/');
        $file_name = '';
        $namespace = '';
        if ($last_ns_pos = strrpos($class_name, '/')) {
            $namespace = substr($class_name, 0, $last_ns_pos);
            $class_name = substr($class_name, $last_ns_pos + 1);
        }
        $file_name = empty($namespace) ? $class_name : $namespace . '/' . $class_name;
        $file_name = str_replace('.php', '', $file_name);

        if (!empty($from_where) && in_array($from_where, array('library', 'helper'))) {
            //add prefix
            $file_name = $from_where . '/' . $file_name;
        }

        $try_ext = array('.php', '.class.php');
        //TODO:暂时只在 P_INC下搜索
        $found = false;
        foreach ($try_ext as $ext) {
            if (file_exists($file_path = P_INC . $file_name . $ext)) {
                include($file_path);
                $found = true;
                break;
            }
            else {
                continue;
            }
        }

        return $found;
    }
    else {
        if (is_array($class_name)) {
            foreach ($class_name as $item) {
                import($item, $from_where);
            }
        }
    }

}

//TODO:since change to composer, disable spl_autoload
//spl_autoload_register('import');

/**
 * 记录系统日志
 * @param $err_level
 * @param $err_str
 * @param $err_file
 * @param $err_line
 */
function my_error_handler($err_level, $err_str, $err_file, $err_line)
{
    $log_file = "PHP_Log_%s-" . date('Ymd') . ".log"; //%s 根据错误级别写入不同的日志文件
    $now = date('Y-m-d H:i:s');
    //日志记录格式
    $log_template = "[$now] %s on line $err_line in file $err_file:$err_str\n";
    $log_record = '';
    switch ($err_level) {
        case E_ERROR:
            $log_file = sprintf($log_file, "Error");
            $log_record = sprintf($log_template, "严重错误");
            break;

        case E_WARNING:
            $log_file = sprintf($log_file, "Warning");
            $log_record = sprintf($log_template, "警告");
            break;

        case E_NOTICE:
            $log_file = sprintf($log_file, "Error");
            $log_record = sprintf($log_template, "提示");
            break;
    }

    if (!empty($log_record)) {
        file_put_contents(P_APP . 'log/' . $log_file, $log_record, FILE_APPEND);
    }

    return;
}

set_error_handler('my_error_handler');

function asset($asset_str = '', $prefix = '', $output = false)
{
    //相对web根目录
    $asset_name = '';
    if (strpos($asset_str, '/') === 0) {
        $asset_name = $prefix . rtrim($asset_str, '/');
    }
    else {
        //所有的静态资源限定在public目录下
        $asset_name = '/' . ltrim($prefix, '/') . trim($asset_str, '/');
    }

    if ($output === true) {
        if (strrpos($asset_name, '.js') > 0) {
            return "<script src='{$asset_name}'></script>";
        }
        if (strrpos($asset_name, '.css') > 0) {
            return "<link rel='stylesheet' href='{$asset_name}' type='text/css'>";
        }
    }

    return $asset_name;

}